﻿namespace OleDbDAL.Processing
{
    using DBUtility;
    using IDal.Processing;
    using Model.Processing;
    using System;
    using System.Data;
    using System.Text;

    public class PackagingFormulaApp : IPackagingFormula
    {
        private string dbname = "Systems";

        public void Add(PackagingFormulaEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_PackagingFormula(");
            builder.Append("ChainNum,FormulaName,FormulaRemark,LastUpdateMan,LastUpdateTime");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.FormulaName + "',");
            builder.Append("'" + model.FormulaRemark + "',");
            builder.Append("" + model.LastUpdateMan + ",");
            builder.Append("'" + model.LastUpdateTime + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int FormulaId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_PackagingFormula ");
            builder.Append(" where FormulaId=" + FormulaId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PackagingFormula ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by FormulaId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "FormulaId", "T_PackagingFormula", swhere);
        }

        public PackagingFormulaEnt GetModel(int FormulaId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select FormulaId,ChainNum,FormulaName,FormulaRemark,LastUpdateMan,LastUpdateTime from T_PackagingFormula ");
            builder.Append(" where FormulaId=" + FormulaId);
            PackagingFormulaEnt ent = new PackagingFormulaEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.FormulaId = FormulaId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.FormulaName = table.Rows[0]["FormulaName"].ToString();
                ent.FormulaRemark = table.Rows[0]["FormulaRemark"].ToString();
                if (table.Rows[0]["LastUpdateMan"].ToString() != "")
                {
                    ent.LastUpdateMan = int.Parse(table.Rows[0]["LastUpdateMan"].ToString());
                }
                if (table.Rows[0]["LastUpdateTime"].ToString() != "")
                {
                    ent.LastUpdateTime = DateTime.Parse(table.Rows[0]["LastUpdateTime"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PackagingFormula ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "FormulaId");
        }

        public void Update(PackagingFormulaEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_PackagingFormula set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("FormulaName='" + model.FormulaName + "',");
            builder.Append("FormulaRemark='" + model.FormulaRemark + "',");
            builder.Append("LastUpdateMan=" + model.LastUpdateMan + ",");
            builder.Append("LastUpdateTime='" + model.LastUpdateTime + "'");
            builder.Append(" where FormulaId=" + model.FormulaId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

